<?php

/**
 * Class build sky of tags range by minimal and maximal count value.
 * And also check range coincidence of current value.
 */
class TagSky
{
	/**
	 * Minimal value for calculate factor range.
	 *
	 * @var integer
	 */
	protected $_minimalCount;

	/**
	 * Maximal value for calculate factor range.
	 *
	 * @var integer
	 */
	protected $_maximalCount;

	/**
	 * Count of differend factor for calculate.
	 *
	 * @var integer
	 */
	protected $_factorCount = 10;

	/**
	 * Array of ranges.
	 *
	 * @var array
	 */
	protected $_ranges = array();

	/**
	 * Check count and change self minimalCount and maximalCount by it.
	 *
	 * @param integer $count
	 * @return void
	 */
	public function setCount($count)
	{
		if ($this->_minimalCount === null) {
			$this->_minimalCount = $count;
			return;
		}

		if ($this->_minimalCount > $count) {
			$this->_minimalCount = $count;
		}

		if ($this->_maximalCount < $count) {
			$this->_maximalCount = $count;
		}
	}

	/**
	 * Return array of calculated ranges for factors.
	 *
	 * @return array
	 */
	protected function _getRanges()
	{
		if (count($this->_ranges)) {
			return $this->_ranges;
		}

		$factorStep = ($this->_maximalCount - $this->_minimalCount) / $this->_factorCount;

		$factor = 0;
		for ($i = $this->_minimalCount; $i < $this->_maximalCount; $i += $factorStep) {
			$this->_ranges[] = array('factor' => $factor++, 'count' => $i);
		}

		return $this->_ranges;
	}

	/**
	 * Get factor by integer value in parameter.
	 *
	 * @param integer $count
	 * @return integer Factor
	 */
	public function getFactor($count)
	{
		$factor = 0;
		$ranges = $this->_getRanges();
		foreach ($ranges as $range) {
			if ($range['count'] > $count) {
				break;
			}

			$factor++;
		}

		return $factor;
	}
}